#pragma once
#include ".\engine.h"
//#include "..\Util\dxutil.h"

CEngine				g_Engine;

CEngine::CEngine(void)
{
    m_fBGCRed = 0.0f;
	m_fBGCGreen = 0.0f;
	m_fBGCBlue = 0.0f;
}

CEngine::~CEngine(void)
{
    
}

BOOL CEngine::Init()
{
	// Init
	if( !m_D3DSetting.Init() )
	{
		OutputDebugString( "Init D3DSetting failed! , CEngine  \n" );
		return FALSE;
	}
	
	if( !m_Timer.Init() )
	{
        OutputDebugString( "Init Timer failed! , CEngine  \n" );
		return FALSE;
	}

	
	if( !m_FontMng.Init() )
	{
		OutputDebugString( "Init FontMng failed! , CEngine  \n" );
		return FALSE;
	}

	
	if( !m_Input.Init() )
	{
		OutputDebugString( "Init Input failed! , CEngine \n" );
		return FALSE;
	}
	

	if( !m_Camera.Init() )
	{
		OutputDebugString( "Init Camera failed! , CEngine \n" );
		return FALSE;
	}

	return TRUE;
}

BOOL CEngine::Frame()
{
	// Operration
	m_Timer.Frame();
	m_Input.Frame();
	m_Camera.Frame();

	if( TRUE == m_Animation.m_bIsLoadData )
	{		
		if( !m_Animation.Frame( g_fSecPerFrame ))
		{
			//DXUtil_Timer( TIMER_RESET );
		}
	}

	RenderStateFrame();

	return TRUE;
}

BOOL CEngine::Render()
{
	SetupLight();
	SetupD3DMateial();

	g_pd3dDevice->Clear( 0 , NULL , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , D3DCOLOR_COLORVALUE( m_fBGCRed / 255.0f , m_fBGCGreen / 255.0f , m_fBGCBlue / 255.0f , 1.0f ) , 1.0f , 0 );

	if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
	{
		// Render
		if( !m_bChangeModel )
		{
			m_ObjectMng.Tick();

			if( m_RenderOption.bRebderBiped && m_Animation.m_bIsLoadData ) 
				m_Animation.Render();
		}

		// Output String
		OutputInfo();

		g_pd3dDevice->EndScene();
	}

	g_pd3dDevice->Present( NULL , NULL , NULL , NULL );

	return TRUE;
}

BOOL CEngine::Release()
{
	// Release + Delete!!
	//m_Light.Release();
	m_ToolMng.mAnimMng.Release();
	m_Animation.Release();
	m_ObjectMng.Release();
	hjREL( m_pVB );	
	//hjREL( m_pIB );
	hjADEL( m_pIndex );

	m_Camera.Release();
	m_Input.Release();
	m_FontMng.Release();
	m_D3DSetting.Release();
	
	return TRUE;
}

/*--------------------------------------------------------------------------------
	Func : Output Information for Debug
--------------------------------------------------------------------------------*/
BOOL	CEngine::OutputInfo()
{
    #ifdef _DEBUG
		RECT rc;
		SetRect( &rc , 0 , 0 , 0 , 0 );
		char Str[64];
		ZeroMemory( Str , sizeof( char ) * 20 );		
		sprintf( Str , "fps : %.2f , SecperFrame : %.5f" , g_fFramePerSec , g_fSecPerFrame );
		g_Engine.m_FontMng.Render( rc , Str );
        
		m_Input.Render( 15 );
		m_Camera.Render( 30 );
	#else
		RECT rc;
		SetRect( &rc , 0 , 0 , 0 , 0 );
		char Str[64];
		ZeroMemory( Str , sizeof( char ) * 20 );		
		sprintf( Str , "fps : %.2f , SecperFrame : %.5f" , g_fFramePerSec , g_fSecPerFrame );
		g_Engine.m_FontMng.Render( rc , Str );

	#endif

	return TRUE;
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CEngine::CreateObjMng( TCHAR* szGeomPath , TCHAR* szMtrlFile , bool bChange )
{
	vector<string> vtTemp;
	vtTemp.clear();
	string	strGeomPath = szGeomPath;
	vtTemp.push_back( strGeomPath );
	m_ObjectMng.Create( vtTemp , szMtrlFile , bChange );
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CEngine::CreateAnim( TCHAR* szAnim )
{
	m_Animation.Init();
	m_ObjectMng.CreateAnim( szAnim );
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CEngine::SetupLight()
{
	D3DLIGHT9 Light;
	ZeroMemory( &Light , sizeof( D3DLIGHT9 ) );
	Light.Diffuse.r  = Light.Ambient.r = Light.Specular.r = 0.7f;
	Light.Diffuse.g  = Light.Ambient.g = Light.Specular.g = 0.7f;
	Light.Diffuse.b  = Light.Ambient.b = Light.Specular.b = 0.7f;
	Light.Diffuse.a  = Light.Ambient.a = Light.Specular.a = 0.7f;

	/*
	Light.Ambient.r = Light.Specular.r = 0.5f;
	Light.Ambient.g = Light.Specular.g = 0.5f;
	Light.Ambient.b = Light.Specular.b = 0.5f;
	Light.Ambient.a = Light.Specular.a = 0.5f;	
	*/

	Light.Range		= 1000.0f;

	switch( m_RenderOption.iLightType )
	{
	case 0:
		{
			Light.Type = D3DLIGHT_DIRECTIONAL;
			D3DXVECTOR3 vDir;
			vDir = D3DXVECTOR3( cosf( GetTickCount() / 700.0f ) , 1.0f , sinf( GetTickCount() / 700.0f ) );
			D3DXVec3Normalize( (D3DXVECTOR3*)&Light.Direction , &vDir );
		}
		break;

	case 1:
		{
			Light.Type	= D3DLIGHT_POINT;
			Light.Position.x = 10.0f;
			Light.Position.y = 0.0f;
			Light.Position.z = 0.0f;
		}
		break;
	}

	g_pd3dDevice->SetLight( 0 , &Light );
	g_pd3dDevice->LightEnable( 0 , TRUE );

	g_pd3dDevice->SetRenderState( D3DRS_AMBIENT , 0xff505050 );
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CEngine::SetupD3DMateial()
{
	D3DMATERIAL9	Mtrl;
	memset( &Mtrl , 0 , sizeof( D3DMATERIAL9 ));
	Mtrl.Ambient.r = Mtrl.Diffuse.r = Mtrl.Specular.r = 0.5f;
	Mtrl.Ambient.g = Mtrl.Diffuse.g = Mtrl.Specular.g = 0.5f;
	Mtrl.Ambient.b = Mtrl.Diffuse.b = Mtrl.Specular.b = 0.5f;
	Mtrl.Ambient.a = Mtrl.Diffuse.a = Mtrl.Specular.a = 0.5f;
	//Mtrl.Power = 1000.0f;
	g_pd3dDevice->SetMaterial( &Mtrl );
}

/*--------------------------------------------------------------------------------
	Func :
--------------------------------------------------------------------------------*/
VOID	CEngine::RenderStateFrame()
{
	if( g_Engine.m_Input.KeyPressed( DIK_1 ) & 0x40 )
	{
		m_RenderOption.bLight = !m_RenderOption.bLight;
	}

	//	LightType Setting
	if( g_Engine.m_Input.KeyPressed( DIK_2 ) & 0x40 )
	{
        g_Engine.m_RenderOption.iLightType++;
        
		if( m_RenderOption.iLightType >= 2 )
			m_RenderOption.iLightType = 0;
	}

    // FILL MODE SETTING
	if( g_Engine.m_Input.KeyPressed( DIK_3 ) & 0x40 )
	{
		g_Engine.m_RenderOption.iFillMode = ( g_Engine.m_RenderOption.iFillMode == D3DFILL_WIREFRAME ? D3DFILL_SOLID  : D3DFILL_WIREFRAME );
	}
}